/* data structure for contexttree. 
 *
 * Copyright (C) 2008 Ed Hong
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#ifndef CONTEXTTREE_H
#define CONTEXTTREE_H

#include "Characteristics.h"
#include "libQccPack.h"

typedef struct intpair {
  int min;
  int max;
} range;  // stores a closed interval [min..max] of ints. 

typedef struct ctnode {
  int charindex;  // characteristic num (index into AllC array) to split on. 
  int charvalue;  // value of characteristic to split on. (can be negative) 
  struct ctnode *left;  // bits w/ char <= charval go to left
  struct ctnode *right; // bits w/ char > charval go to right. 
  int leafnum; // -1 if not a leaf node, the context num to use for coding
               // otherwise. 
  range *charranges; // current interval applicable for this node; 
                      // indexed by "on disk" char ID.  These value are 
                      // not always set; they are currently set and used
                      // only in encode/decode ctd trees. 
} contexttreenode;

typedef struct {
  contexttreenode * root; 
  int numleafnodes;
  int num_on_disk_chars; // total number of char IDs in use (those "on disk");
       // == # of 1 bits in charset passed in as input.    
  int ContextIDMap[TOTAL_NUM_CHARACTERISTICS]; // index i contains
     // actual characteristic number of "on disk" char ID #i; 
  int reverseContextIDMap[TOTAL_NUM_CHARACTERISTICS];
     // index i contains "on disk" number of characteristic i; 
} contexttree; 

int find_context_num(const 
		     SubbandComponent *sc, int componentnnum, // component num for color images
		     // with color images, assume sc is an array of size . 
		     int r, int c, int sb, int bp, const contexttree *ct);

// assume memory for out already created.
int create_fullsplitcontexttree(const unsigned long long *charset, contexttree *out); // tree that splits on 
// all context characteristics in charset. 

int create_imgsplitcontexttree(const unsigned long long *charset, contexttree *out); // tree that splits on 
// all context characteristics in charset, except that does not fully split by is_significance; 
// one context created for all refinement and all sign bits. 
//   creates fullsplit on the remaming flags under the significance bits. 


void printcontexttree(const contexttree *x) ;  // prints out info on all nodes
void printtree(const contexttree *x) ;  // prints out parenthesized info for "tree" preprocessor. 

int readctdfile(const char *filename, const unsigned long long *charset, contexttree *out) ;
int printctdfile(const char *filename, const contexttree *out);


int encodectd_bitbuffer(QccBitBuffer *output_buffer, const contexttree *out);
 
int decodectd_bitbuffer(QccBitBuffer *input_buffer, const unsigned long long *charset,
                        contexttree *out);
                        
void freecontexttreenodes(contexttree*x); // recursively frees contexttreenodes contained in x;
    //  does not free toplevel contexttree data structure. 
    
#endif /* CONTEXTTREE_H */
